package gq.example.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import gq.example.constant.MessageConstant;
import gq.example.dto.FeedbackDto;
import gq.example.dto.FeedbackPageQueryDto;
import gq.example.entity.Feedback;
import gq.example.entity.Question;
import gq.example.entity.QuestionAndFeedback;
import gq.example.mapper.FeedbackMapper;
import gq.example.mapper.QuestionMapper;
import gq.example.result.PageResult;
import gq.example.result.Result;
import gq.example.service.FeedbackService;
import gq.example.service.IUsersService;
import gq.example.vo.CommunicationVo;
import gq.example.vo.FeedbackVo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class FeedbackServiceImpl implements FeedbackService {
    @Resource
    private FeedbackMapper feedbackMapper;

    @Resource
    private QuestionMapper questionMapper;

    @Resource
    private  IUsersService iUsersService;

    /**
     * 回复查看(大体)
     * @param feedbackPageQueryDto
     * @return
     */
    @Override
    public PageResult pageQuery(FeedbackPageQueryDto feedbackPageQueryDto) {
        PageHelper.startPage(feedbackPageQueryDto.getPage(), feedbackPageQueryDto.getSize());

        Page<FeedbackVo> page = feedbackMapper.pageQuery(feedbackPageQueryDto);

        //封装结果集
        long total = page.getTotal();
        List<FeedbackVo> records = page.getResult();

        return new PageResult(total, records);
    }

    /**
     * 回复查看(详细)
     * @param createUser
     * @return
     */
    @Override
    public  List<CommunicationVo> viewReply(Integer questionId, Long createUser) {
        //存在未读才更新
        if(feedbackMapper.existsFeedbackMatch(questionId)){

            //将对应的未读回复更新为已读
            if(feedbackMapper.existsProposerIdMatch(questionId, createUser)){
                feedbackMapper.updateRelationProposer(questionId); // 提出者更新未读
            }else{
                feedbackMapper.updateRelationReplier(questionId); // 回复者更新未读
            }
        }
        //根据回复id，以及回复人获取回复
        return feedbackMapper.viewReply(questionId);
    }

    /**
     * 判断是否归档
     * @param questionId
     * @return
     */
    @Override
    public boolean isArchived(Integer questionId) {
        return feedbackMapper.isArchived(questionId);
    }

    /**
     * 回复意见
     * @param feedbackDto
     * @return
     */
    @Override
    public void addFeedback(FeedbackDto feedbackDto) {
        //创建反馈对象
        Feedback feedback = new Feedback();

        //拷贝前端数据
        BeanUtils.copyProperties(feedbackDto, feedback);

        //更新意见状态
        Question question = Question.builder()
                .id(feedbackDto.getQuestionId())
                .status(MessageConstant.INDICATES_REPLY) // 状态设为已回复
                .build();
        questionMapper.update(question);

        //插入回复数据
        feedbackMapper.addFeedback(feedback);

        //建立意见与回复的关系
        QuestionAndFeedback questionAndFeedback = QuestionAndFeedback.builder()
                .isRead(MessageConstant.isNotRead) // 默认未读
                .questionId(feedbackDto.getQuestionId())
                .feedbackId(feedback.getId())
                .proposerId(Long.valueOf(questionMapper.getCreateUserById(feedbackDto.getQuestionId())))
                .replierId(feedbackDto.getCreateUser())
                .proposerName(iUsersService.getNameById(questionMapper.getCreateUserById(question.getId())))// 提出人姓名
                .replierName(iUsersService.getNameById(Math.toIntExact(feedbackDto.getCreateUser()))) // 回复人姓名
                .createTime(LocalDateTime.now())
                .build();
        feedbackMapper.buildRelation(questionAndFeedback);
    }

    /**
     * 用户未读回复数量
     * @param createUser
     * @return
     */
    @Override
    public Integer countReplies(Long createUser) {
        return feedbackMapper.countReplies(createUser);
    }

    /**
     * 用户未读追问数量
     * @param createUser
     * @param sectorId
     * @return
     */
    @Override
    public Integer countProbe(Long createUser, Integer sectorId) {
        return feedbackMapper.countProbe(createUser, sectorId);
    }


}
